{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9080b39e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import logging, sys\n",
    "\n",
    "# logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "# logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n",
    "\n",
    "# Uncomment if you want to temporarily disable logger\n",
    "logging.disable(sys.maxsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b4b4387b-413e-4016-ba1e-88b3d9410a38",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# fetch \"New York City\" page from Wikipedia\n",
    "from pathlib import Path\n",
    "\n",
    "import requests\n",
    "\n",
    "response = requests.get(\n",
    "    \"https://en.wikipedia.org/w/api.php\",\n",
    "    params={\n",
    "        \"action\": \"query\",\n",
    "        \"format\": \"json\",\n",
    "        \"titles\": \"New York City\",\n",
    "        \"prop\": \"extracts\",\n",
    "        # 'exintro': True,\n",
    "        \"explaintext\": True,\n",
    "    },\n",
    ").json()\n",
    "page = next(iter(response[\"query\"][\"pages\"].values()))\n",
    "nyc_text = page[\"extract\"]\n",
    "\n",
    "data_path = Path(\"data\")\n",
    "if not data_path.exists():\n",
    "    Path.mkdir(data_path)\n",
    "\n",
    "with open(\"data/nyc_text.txt\", \"w\") as fp:\n",
    "    fp.write(nyc_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f1a9eb90-335c-4214-8bb6-fd1edbe3ccbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# My OpenAI Key\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8d0b2364-4806-4656-81e7-3f6e4b910b5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import TreeIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext\n",
    "from llama_index.logger import LlamaLogger\n",
    "from llama_index.llms import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4766ac56-ac8d-4f33-b994-6901964241ea",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# gpt-3 (davinci)\n",
    "gpt3 = OpenAI(temperature=0, model=\"text-davinci-003\")\n",
    "service_context_gpt3 = ServiceContext.from_defaults(llm=gpt3)\n",
    "\n",
    "# gpt-4\n",
    "gpt4 = OpenAI(temperature=0, model=\"gpt-4\")\n",
    "service_context_gpt4 = ServiceContext.from_defaults(llm=gpt4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "523fbebe-6e79-4d7b-b400-188b711a0e8f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "documents = SimpleDirectoryReader(\"data\").load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0e50407-1838-45d8-83af-894576245f03",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "index = TreeIndex.from_documents(documents, service_context=service_context_gpt4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4fc3f18a-0ef9-453c-acf8-7aedd784cdcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: What battles took place in New York City in the American Revolution?\n",
      ">[Level 0] Current response: ANSWER: 2\n",
      "\n",
      "This summary was selected because it discusses the history of New York City, including significant events during the American Revolution, such as the Battle of Long Island. The other summaries do not mention battles or the American Revolution.\n",
      ">[Level 0] Selected node: [2]/[2]\n",
      ">[Level 0] Node [2] Summary text: New York City has a rich history, from its beginnings as a Dutch trading post to its growth into a major global city. Slavery played a significant role in the city's early economy, and the African Burying Ground was discovered during construction in the 1990s. The American Revolution saw significant events in New York, including the Battle of Long Island and the establishment of the city as the national capital. Throughout the 19th century, the city's population grew rapidly, and it became a center for industry, commerce, and immigration. The 20th century saw the city's continued growth and influence, with events such as the Stonewall riots and the September 11, 2001 attacks shaping its history. In the 21st century, New York City has faced challenges such as Hurricane Sandy and the COVID-19 pandemic, but it remains a global center for culture, finance, and innovation.\n",
      ">[Level 1] Current response: ANSWER: 1\n",
      "\n",
      "This summary was selected because it mentions the Battle of Long Island, which was the largest battle of the American Revolutionary War and took place in New York City.\n",
      ">[Level 1] Selected node: [1]/[1]\n",
      ">[Level 1] Node [1] Summary text: South Carolina. Most cases were that of domestic slavery, as a New York household then commonly enslaved few or several people. Others were hired out to work at labor. Slavery became integrally tied to New York's economy through the labor of slaves throughout the port, and the banking and shipping industries trading with the American South. During construction in Foley Square in the 1990s, the African Burying Ground was discovered; the cemetery included 10,000 to 20,000 of graves of colonial-era Africans, some enslaved and some free.The 1735 trial and acquittal in Manhattan of John Peter Zenger, who had been accused of seditious libel after criticizing colonial governor William Cosby, helped to establish the freedom of the press in North America. In 1754, Columbia University was founded under charter by King George II as King's College in Lower Manhattan.\n",
      "\n",
      "\n",
      "=== American Revolution ===\n",
      "\n",
      "The Stamp Act Congress met in New York in October 1765, as the Sons of Liberty organization emerged in the city and skirmished over the next ten years with British troops stationed there. The Battle of Long Island, the largest battle of the American Revolutionary War, was fought in August 1776 within the modern-day borough of Brooklyn. After the battle, in which the Americans were defeated, the British made the city their military and political base of operations in North America. The city was a haven for Loyalist refugees and escaped slaves who joined the British lines for freedom\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: South Carolina. Most cases were that of domestic slavery, as a New York household then commonly enslaved few or several people. Others were hired out to work at labor. Slavery became integrally tie...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(service_context=service_context_gpt4, verbose=True)\n",
    "response_gpt4 = query_engine.query(\n",
    "    \"What battles took place in New York City in the American Revolution?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5195f269-e362-4eb2-bf17-533d0a149bca",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The Battle of Long Island took place in New York City in the American Revolution.'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c59c8fb9-4a06-4686-80c3-e18e990e2ae1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SourceNode(source_text=\"South Carolina. Most cases were that of domestic slavery, as a New York household then commonly enslaved few or several people. Others were hired out to work at labor. Slavery became integrally tied to New York's economy through the labor of slaves throughout the port, and the banking and shipping industries trading with the American South. During construction in Foley Square in the 1990s, the African Burying Ground was discovered; the cemetery included 10,000 to 20,000 of graves of colonial-era Africans, some enslaved and some free.The 1735 trial and acquittal in Manhattan of John Peter Zenger, who had been accused of seditious libel after criticizing colonial governor William Cosby, helped to establish the freedom of the press in North America. In 1754, Columbia University was founded under charter by King George II as King's College in Lower Manhattan.\\n\\n\\n=== American Revolution ===\\n\\nThe Stamp Act Congress met in New York in October 1765, as the Sons of Liberty organization emerged in the city and skirmished over the next ten years with British troops stationed there. The Battle of Long Island, the largest battle of the American Revolutionary War, was fought in August 1776 within the modern-day borough of Brooklyn. After the battle, in which the Americans were defeated, the British made the city their military and political base of operations in North America. The city was a haven for Loyalist refugees and escaped slaves who joined the British lines for freedom\", doc_id='43371b31-6570-4843-bdcb-64f3e19fe85f', extra_info=None, node_info=None, similarity=None, image=None)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response_gpt4.source_nodes[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "09fe5bd9-7571-4ff9-a548-0015c1894989",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: What battles took place in New York City in the American Revolution?\n",
      ">[Level 0] Current response: \n",
      "ANSWER: 2. This summary was selected because it mentions the American Revolution and the significant events that took place in New York City during this time, such as the Battle of Long Island and the establishment of the city as the national capital.\n",
      ">[Level 0] Selected node: [2]/[2]\n",
      ">[Level 0] Node [2] Summary text: New York City has a rich history, from its beginnings as a Dutch trading post to its growth into a major global city. Slavery played a significant role in the city's early economy, and the African Burying Ground was discovered during construction in the 1990s. The American Revolution saw significant events in New York, including the Battle of Long Island and the establishment of the city as the national capital. Throughout the 19th century, the city's population grew rapidly, and it became a center for industry, commerce, and immigration. The 20th century saw the city's continued growth and influence, with events such as the Stonewall riots and the September 11, 2001 attacks shaping its history. In the 21st century, New York City has faced challenges such as Hurricane Sandy and the COVID-19 pandemic, but it remains a global center for culture, finance, and innovation.\n",
      ">[Level 1] Current response: \n",
      "ANSWER: 5. The Battle of Long Island, the largest battle of the American Revolutionary War, was fought in August 1776 within the modern-day borough of Brooklyn. This summary was selected because it is the only one that mentions the Battle of Long Island, which was a battle that took place in New York City during the American Revolution.\n",
      ">[Level 1] Selected node: [5]/[5]\n",
      ">[Level 1] Node [5] Summary text: but his proposal was not acted on. Anger at new military conscription laws during the American Civil War (1861–1865), which spared wealthier men who could afford to pay a $300 (equivalent to $6,602 in 2021) commutation fee to hire a substitute, led to the Draft Riots of 1863, whose most visible participants were ethnic Irish working class.The draft riots deteriorated into attacks on New York's elite, followed by attacks on Black New Yorkers and their property after fierce competition for a decade between Irish immigrants and Black people for work. Rioters burned the Colored Orphan Asylum to the ground, with more than 200 children escaping harm due to efforts of the New York Police Department, which was mainly made up of Irish immigrants. At least 120 people were killed. Eleven Black men were lynched over five days, and the riots forced hundreds of Blacks to flee the city for Williamsburg, Brooklyn, and New Jersey. The Black population in Manhattan fell below 10,000 by 1865, which it had last been in 1820. The White working class had established dominance. Violence by longshoremen against Black men was especially fierce in the docks area. It was one of the worst incidents of civil unrest in American history.In 1898, the City of New York was formed with the consolidation of Brooklyn (until then a separate city), the County of New York (which then included parts of the Bronx), the County of Richmond, and the western portion of the County of Queens. The opening of\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: but his proposal was not acted on. Anger at new military conscription laws during the American Civil War (1861–1865), which spared wealthier men who could afford to pay a $300 (equivalent to $6,602...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(service_context=service_context_gpt3, verbose=True)\n",
    "response_gpt3 = query_engine.query(\n",
    "    \"What battles took place in New York City in the American Revolution?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "345e70dd-2b9f-4966-8ee2-36fa40e9e598",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'No battles took place in New York City during the American Revolution. The city was occupied by British forces from 1776 to 1783. The Battle of Long Island (1776), the Battle of White Plains (1776), and the Battle of Fort Washington (1776) all took place in the vicinity of New York City, but not within the city itself.'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "461aee65-7c72-44a8-835f-32e2e5fb5935",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SourceNode(source_text=\"but his proposal was not acted on. Anger at new military conscription laws during the American Civil War (1861–1865), which spared wealthier men who could afford to pay a $300 (equivalent to $6,602 in 2021) commutation fee to hire a substitute, led to the Draft Riots of 1863, whose most visible participants were ethnic Irish working class.The draft riots deteriorated into attacks on New York's elite, followed by attacks on Black New Yorkers and their property after fierce competition for a decade between Irish immigrants and Black people for work. Rioters burned the Colored Orphan Asylum to the ground, with more than 200 children escaping harm due to efforts of the New York Police Department, which was mainly made up of Irish immigrants. At least 120 people were killed. Eleven Black men were lynched over five days, and the riots forced hundreds of Blacks to flee the city for Williamsburg, Brooklyn, and New Jersey. The Black population in Manhattan fell below 10,000 by 1865, which it had last been in 1820. The White working class had established dominance. Violence by longshoremen against Black men was especially fierce in the docks area. It was one of the worst incidents of civil unrest in American history.In 1898, the City of New York was formed with the consolidation of Brooklyn (until then a separate city), the County of New York (which then included parts of the Bronx), the County of Richmond, and the western portion of the County of Queens. The opening of\", doc_id='43371b31-6570-4843-bdcb-64f3e19fe85f', extra_info=None, node_info=None, similarity=None, image=None)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response_gpt3.source_nodes[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "97f3ddf1-8dc2-4fb8-831f-2c06649e0955",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: What are the airports in New York City?\n",
      ">[Level 0] Current response: ANSWER: 9\n",
      "\n",
      "This summary was selected because it mentions that New York City is served by three major airports and discusses plans to expand and improve facilities at LaGuardia Airport. None of the other summaries specifically mention airports in the city.\n",
      ">[Level 0] Selected node: [9]/[9]\n",
      ">[Level 0] Node [9] Summary text: New York City is known for its efforts in environmental sustainability, with initiatives such as the Citi Bike program, green office buildings, and a commitment to reducing greenhouse gas emissions. The city's water supply comes from the protected Catskill Mountains watershed, providing pure drinking water without the need for purification. New York City's air quality is within the recommended limits of the World Health Organization, and efforts are being made to revitalize polluted areas like Newtown Creek. The city's government operates under a strong mayor-council system, with the current mayor being Eric Adams. The Democratic Party holds the majority of public offices in the city.\n",
      "\n",
      "New York City's extensive transportation system includes the largest rapid transit system in the world, the New York City Subway, as well as the largest public bus fleet in North America. The city is served by three major airports, with plans to expand and improve facilities at LaGuardia Airport. The Staten Island Ferry is the world's busiest ferry route, and other ferry systems connect various parts of the city. Taxis, vehicles for hire, and trams are also part of the city's transportation infrastructure. Despite heavy reliance on public transit, streets and highways remain a defining feature of New York City, with congestion pricing set to go into effect in 2022\n",
      ">[Level 1] Current response: ANSWER: 9\n",
      "\n",
      "This summary was selected because it specifically mentions the airports in New York City, including John F. Kennedy International Airport, Newark Liberty International Airport, and LaGuardia Airport. It also provides information about their passenger volumes and international rankings.\n",
      ">[Level 1] Selected node: [9]/[9]\n",
      ">[Level 1] Node [9] Summary text: were the busiest and fourth busiest U.S. gateways for international air passengers, respectively, in 2012; as of 2011, JFK was the busiest airport for international passengers in North America.Plans have advanced to expand passenger volume at a fourth airport, Stewart International Airport near Newburgh, New York, by the Port Authority of New York and New Jersey. Plans were announced in July 2015 to entirely rebuild LaGuardia Airport in a multibillion-dollar project to replace its aging facilities. Other commercial airports in or serving the New York metropolitan area include Long Island MacArthur Airport, Trenton–Mercer Airport and Westchester County Airport. The primary general aviation airport serving the area is Teterboro Airport.\n",
      "\n",
      "\n",
      "=== Ferries ===\n",
      "\n",
      "The Staten Island Ferry is the world's busiest ferry route, carrying more than 23 million passengers from July 2015 through June 2016 on the 5.2-mile (8.4 km) route between Staten Island and Lower Manhattan and running 24 hours a day. Other ferry systems shuttle commuters between Manhattan and other locales within the city and the metropolitan area.\n",
      "NYC Ferry, a NYCEDC initiative with routes planned to travel to all five boroughs, was launched in 2017, with second graders choosing the names of the ferries. Meanwhile, Seastreak ferry announced construction of a 600-passenger high-speed luxury ferry in September 2016, to shuttle riders between the Jersey Shore and Manhattan,\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: were the busiest and fourth busiest U.S. gateways for international air passengers, respectively, in 2012; as of 2011, JFK was the busiest airport for international passengers in North America.Plan...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(service_context=service_context_gpt4, verbose=True)\n",
    "\n",
    "response_gpt4 = query_engine.query(\n",
    "    \"What are the airports in New York City?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1ee5e6b7-1691-42f9-abc8-a83422939d43",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The context information does not provide a complete list of airports in New York City. However, it mentions John F. Kennedy International Airport (JFK), LaGuardia Airport, and Stewart International Airport near Newburgh, New York. Other airports mentioned in the context serving the New York metropolitan area include Long Island MacArthur Airport, Trenton–Mercer Airport, Westchester County Airport, and Teterboro Airport (a general aviation airport).'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "299073fd-74d5-4f0b-8861-a18362888371",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"were the busiest and fourth busiest U.S. gateways for international air passengers, respectively, in 2012; as of 2011, JFK was the busiest airport for international passengers in North America.Plans have advanced to expand passenger volume at a fourth airport, Stewart International Airport near Newburgh, New York, by the Port Authority of New York and New Jersey. Plans were announced in July 2015 to entirely rebuild LaGuardia Airport in a multibillion-dollar project to replace its aging facilities. Other commercial airports in or serving the New York metropolitan area include Long Island MacArthur Airport, Trenton–Mercer Airport and Westchester County Airport. The primary general aviation airport serving the area is Teterboro Airport.\\n\\n\\n=== Ferries ===\\n\\nThe Staten Island Ferry is the world's busiest ferry route, carrying more than 23 million passengers from July 2015 through June 2016 on the 5.2-mile (8.4 km) route between Staten Island and Lower Manhattan and running 24 hours a day. Other ferry systems shuttle commuters between Manhattan and other locales within the city and the metropolitan area.\\nNYC Ferry, a NYCEDC initiative with routes planned to travel to all five boroughs, was launched in 2017, with second graders choosing the names of the ferries. Meanwhile, Seastreak ferry announced construction of a 600-passenger high-speed luxury ferry in September 2016, to shuttle riders between the Jersey Shore and Manhattan,\""
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response_gpt4.source_nodes[0].source_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1131cbd6-6799-4b26-9c48-e7f09718a1e1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: What are the airports in New York City?\n",
      ">[Level 0] Current response: \n",
      "ANSWER: 8. This summary provides information about the city's transportation system, which includes three major airports. It also mentions plans to expand and improve facilities at LaGuardia Airport, which indicates that the airports in New York City are a current topic of discussion.\n",
      ">[Level 0] Selected node: [8]/[8]\n",
      ">[Level 0] Node [8] Summary text: New York City is home to numerous cultural institutions, historic sites, and diverse cuisine influenced by its immigrant history. The city is known for its street parades, distinctive regional accent, and sports teams across various leagues. Environmental issues are a concern due to the city's size and density, but efforts have been made to reduce its environmental impact and carbon footprint. New York City has a high rate of public transit use, a growing number of cyclists, and is considered the most walkable large city in the United States.\n",
      ">[Level 1] Current response: \n",
      "ANSWER: 10. This summary does not mention any airports in New York City, so it is not relevant to the question.\n",
      ">[Level 1] Selected node: [10]/[10]\n",
      ">[Level 1] Node [10] Summary text: it is one of the country's biggest sources of pollution and has the lowest per-capita greenhouse gas emissions rate and electricity usage. Governors Island is planned to host a US$1 billion research and education center to make New York City the global leader in addressing the climate crisis.\n",
      "\n",
      "\n",
      "=== Environmental impact reduction ===\n",
      "New York City has focused on reducing its environmental impact and carbon footprint. Mass transit use in New York City is the highest in the United States. Also, by 2010, the city had 3,715 hybrid taxis and other clean diesel vehicles, representing around 28% of New York's taxi fleet in service, the most of any city in North America. New York City is the host of Climate Week NYC, the largest Climate Week to take place globally and regarded as major annual climate summit.\n",
      "New York's high rate of public transit use, more than 200,000 daily cyclists as of 2014, and many pedestrian commuters make it the most energy-efficient major city in the United States. Walk and bicycle modes of travel account for 21% of all modes for trips in the city; nationally the rate for metro regions is about 8%. In both its 2011 and 2015 rankings, Walk Score named New York City the most walkable large city in the United States, and in 2018, Stacker ranked New York the most walkable U.S. city. Citibank sponsored the introduction of 10,000 public bicycles for the city's bike-share project in the summer of 2013. New York City's numerical \"in-season\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: it is one of the country's biggest sources of pollution and has the lowest per-capita greenhouse gas emissions rate and electricity usage. Governors Island is planned to host a US$1 billion researc...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(service_context=service_context_gpt3, verbose=True)\n",
    "\n",
    "response_gpt3 = query_engine.query(\n",
    "    \"What are the airports in New York City?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "66094f18-b7d6-4ee9-afcc-4622b73015ac",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The airports in New York City are John F. Kennedy International Airport (JFK), LaGuardia Airport (LGA), Newark Liberty International Airport (EWR), and Stewart International Airport (SWF).\n"
     ]
    }
   ],
   "source": [
    "print(str(response_gpt3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9231e016-f5cc-409a-b362-d0a4155d1837",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'it is one of the country\\'s biggest sources of pollution and has the lowest per-capita greenhouse gas emissions rate and electricity usage. Governors Island is planned to host a US$1 billion research and education center to make New York City the global leader in addressing the climate crisis.\\n\\n\\n=== Environmental impact reduction ===\\nNew York City has focused on reducing its environmental impact and carbon footprint. Mass transit use in New York City is the highest in the United States. Also, by 2010, the city had 3,715 hybrid taxis and other clean diesel vehicles, representing around 28% of New York\\'s taxi fleet in service, the most of any city in North America. New York City is the host of Climate Week NYC, the largest Climate Week to take place globally and regarded as major annual climate summit.\\nNew York\\'s high rate of public transit use, more than 200,000 daily cyclists as of 2014, and many pedestrian commuters make it the most energy-efficient major city in the United States. Walk and bicycle modes of travel account for 21% of all modes for trips in the city; nationally the rate for metro regions is about 8%. In both its 2011 and 2015 rankings, Walk Score named New York City the most walkable large city in the United States, and in 2018, Stacker ranked New York the most walkable U.S. city. Citibank sponsored the introduction of 10,000 public bicycles for the city\\'s bike-share project in the summer of 2013. New York City\\'s numerical \"in-season'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response_gpt3.source_nodes[0].source_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53265fd4-da98-4cf9-abfb-3f76105fd2ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "query_engine = index.as_query_engine(service_context=service_context_gpt4, verbose=True)\n",
    "\n",
    "response_gpt4 = query_engine.query(\n",
    "    \"What battles took place in New York City in the American Revolution?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29de7f3c-5c10-4d8c-b809-5cb300e4ed8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(str(response_gpt4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a5299e5-cb79-4961-abc2-dacdce76a507",
   "metadata": {},
   "outputs": [],
   "source": [
    "response_gpt4.source_nodes[0].source_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "edea530f-03d1-4599-908c-c6f5808e23b9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: Who is the current mayor of New York City?\n",
      ">[Level 0] Current response: ANSWER: 9\n",
      "\n",
      "This summary was selected because it specifically mentions the current mayor of New York City, Eric Adams.\n",
      ">[Level 0] Selected node: [9]/[9]\n",
      ">[Level 0] Node [9] Summary text: New York City is known for its efforts in environmental sustainability, with initiatives such as the Citi Bike program, green office buildings, and a commitment to reducing greenhouse gas emissions. The city's water supply comes from the protected Catskill Mountains watershed, providing pure drinking water without the need for purification. New York City's air quality is within the recommended limits of the World Health Organization, and efforts are being made to revitalize polluted areas like Newtown Creek. The city's government operates under a strong mayor-council system, with the current mayor being Eric Adams. The Democratic Party holds the majority of public offices in the city.\n",
      "\n",
      "New York City's extensive transportation system includes the largest rapid transit system in the world, the New York City Subway, as well as the largest public bus fleet in North America. The city is served by three major airports, with plans to expand and improve facilities at LaGuardia Airport. The Staten Island Ferry is the world's busiest ferry route, and other ferry systems connect various parts of the city. Taxis, vehicles for hire, and trams are also part of the city's transportation infrastructure. Despite heavy reliance on public transit, streets and highways remain a defining feature of New York City, with congestion pricing set to go into effect in 2022\n",
      ">[Level 1] Current response: ANSWER: 5\n",
      "\n",
      "This summary was selected because it mentions the current mayor of New York City, Eric Adams, who was elected in 2021 and assumed office on January 1, 2022.\n",
      ">[Level 1] Selected node: [5]/[5]\n",
      ">[Level 1] Node [5] Summary text: carried by a Republican  presidential election since President Calvin Coolidge won the five boroughs in 1924. A Republican candidate for statewide office has not won all five boroughs of the city since it was incorporated in 1898. In 2012, Democrat Barack Obama became the first presidential candidate of any party to receive more than 80% of the overall vote in New York City, sweeping all five boroughs. Party platforms center on affordable housing, education, and economic development, and labor politics are of importance in the city. Thirteen out of 27 U.S. congressional districts in the state of New York include portions of New York City.New York is one of the most important sources of political fundraising in the United States. At least four of the top five ZIP Codes in the nation for political contributions were in Manhattan for the 2004, 2006, and 2008 elections. The top ZIP Code, 10021 on the Upper East Side, generated the most money for the 2004 presidential campaigns of George W. Bush and John Kerry. The city has a strong imbalance of payments with the national and state governments. It receives 83 cents in services for every $1 it sends to the federal government in taxes (or annually sends $11.4 billion more than it receives back). City residents and businesses also sent an additional $4.1 billion in the 2009–2010 fiscal year to the state of New York than the city received in return.\n",
      "\n",
      "\n",
      "== Transportation ==\n",
      "\n",
      "New York City's comprehensive transportation system is both\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: carried by a Republican  presidential election since President Calvin Coolidge won the five boroughs in 1924. A Republican candidate for statewide office has not won all five boroughs of the city s...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "response_gpt4 = query_engine.query(\n",
    "    \"Who is the current mayor of New York City?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e4117466-8002-4211-84a2-157cf8410076",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The context information does not provide the name of the current mayor of New York City.\n"
     ]
    }
   ],
   "source": [
    "print(str(response_gpt4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c80e1998-cabe-46f1-863f-2c95cc0e59c1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"carried by a Republican  presidential election since President Calvin Coolidge won the five boroughs in 1924. A Republican candidate for statewide office has not won all five boroughs of the city since it was incorporated in 1898. In 2012, Democrat Barack Obama became the first presidential candidate of any party to receive more than 80% of the overall vote in New York City, sweeping all five boroughs. Party platforms center on affordable housing, education, and economic development, and labor politics are of importance in the city. Thirteen out of 27 U.S. congressional districts in the state of New York include portions of New York City.New York is one of the most important sources of political fundraising in the United States. At least four of the top five ZIP Codes in the nation for political contributions were in Manhattan for the 2004, 2006, and 2008 elections. The top ZIP Code, 10021 on the Upper East Side, generated the most money for the 2004 presidential campaigns of George W. Bush and John Kerry. The city has a strong imbalance of payments with the national and state governments. It receives 83 cents in services for every $1 it sends to the federal government in taxes (or annually sends $11.4 billion more than it receives back). City residents and businesses also sent an additional $4.1 billion in the 2009–2010 fiscal year to the state of New York than the city received in return.\\n\\n\\n== Transportation ==\\n\\nNew York City's comprehensive transportation system is both\""
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response_gpt4.source_nodes[0].source_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "482f7203-c163-4b51-905e-333d52297c71",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: Who is the current mayor of New York City?\n",
      ">[Level 0] Current response: \n",
      "ANSWER: 8. This summary provides information about the city's government, which includes the current mayor, Eric Adams.\n",
      ">[Level 0] Selected node: [8]/[8]\n",
      ">[Level 0] Node [8] Summary text: New York City is home to numerous cultural institutions, historic sites, and diverse cuisine influenced by its immigrant history. The city is known for its street parades, distinctive regional accent, and sports teams across various leagues. Environmental issues are a concern due to the city's size and density, but efforts have been made to reduce its environmental impact and carbon footprint. New York City has a high rate of public transit use, a growing number of cyclists, and is considered the most walkable large city in the United States.\n",
      ">[Level 1] Current response: \n",
      "ANSWER: 10. This summary does not provide any information about the current mayor of New York City, so it is not relevant to the question.\n",
      ">[Level 1] Selected node: [10]/[10]\n",
      ">[Level 1] Node [10] Summary text: it is one of the country's biggest sources of pollution and has the lowest per-capita greenhouse gas emissions rate and electricity usage. Governors Island is planned to host a US$1 billion research and education center to make New York City the global leader in addressing the climate crisis.\n",
      "\n",
      "\n",
      "=== Environmental impact reduction ===\n",
      "New York City has focused on reducing its environmental impact and carbon footprint. Mass transit use in New York City is the highest in the United States. Also, by 2010, the city had 3,715 hybrid taxis and other clean diesel vehicles, representing around 28% of New York's taxi fleet in service, the most of any city in North America. New York City is the host of Climate Week NYC, the largest Climate Week to take place globally and regarded as major annual climate summit.\n",
      "New York's high rate of public transit use, more than 200,000 daily cyclists as of 2014, and many pedestrian commuters make it the most energy-efficient major city in the United States. Walk and bicycle modes of travel account for 21% of all modes for trips in the city; nationally the rate for metro regions is about 8%. In both its 2011 and 2015 rankings, Walk Score named New York City the most walkable large city in the United States, and in 2018, Stacker ranked New York the most walkable U.S. city. Citibank sponsored the introduction of 10,000 public bicycles for the city's bike-share project in the summer of 2013. New York City's numerical \"in-season\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: it is one of the country's biggest sources of pollution and has the lowest per-capita greenhouse gas emissions rate and electricity usage. Governors Island is planned to host a US$1 billion researc...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(service_context=service_context_gpt3, verbose=True)\n",
    "\n",
    "response_gpt3 = query_engine.query(\n",
    "    \"Who is the current mayor of New York City?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "de045d47-b3cd-4a37-980f-febeee97beb6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The current mayor of New York City is Bill de Blasio.\n"
     ]
    }
   ],
   "source": [
    "print(str(response_gpt3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "694456a7-d2c1-4be4-9e41-2b5eadaf9c23",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'it is one of the country\\'s biggest sources of pollution and has the lowest per-capita greenhouse gas emissions rate and electricity usage. Governors Island is planned to host a US$1 billion research and education center to make New York City the global leader in addressing the climate crisis.\\n\\n\\n=== Environmental impact reduction ===\\nNew York City has focused on reducing its environmental impact and carbon footprint. Mass transit use in New York City is the highest in the United States. Also, by 2010, the city had 3,715 hybrid taxis and other clean diesel vehicles, representing around 28% of New York\\'s taxi fleet in service, the most of any city in North America. New York City is the host of Climate Week NYC, the largest Climate Week to take place globally and regarded as major annual climate summit.\\nNew York\\'s high rate of public transit use, more than 200,000 daily cyclists as of 2014, and many pedestrian commuters make it the most energy-efficient major city in the United States. Walk and bicycle modes of travel account for 21% of all modes for trips in the city; nationally the rate for metro regions is about 8%. In both its 2011 and 2015 rankings, Walk Score named New York City the most walkable large city in the United States, and in 2018, Stacker ranked New York the most walkable U.S. city. Citibank sponsored the introduction of 10,000 public bicycles for the city\\'s bike-share project in the summer of 2013. New York City\\'s numerical \"in-season'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response_gpt3.source_nodes[0].source_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6fd96b3a-bea8-4fe4-be28-56f0ea218d4f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: What is the demographic breakdown of NYC by ethnicity?\n",
      ">[Level 0] Current response: ANSWER: 4\n",
      "\n",
      "This summary was selected because it provides the demographic breakdown of New York City by ethnicity, including percentages for White (non-Hispanic), Hispanic or Latino, Black or African American (non-Hispanic), Asian, and Native American (non-Hispanic) populations.\n",
      ">[Level 0] Selected node: [4]/[4]\n",
      ">[Level 0] Node [4] Summary text: New York City experiences a humid subtropical climate with cold winters and hot summers. The city receives 49.5 inches of precipitation annually, and average winter snowfall is 29.8 inches. Hurricanes and tropical storms are rare, but Hurricane Sandy in 2012 prompted discussions of constructing seawalls and coastal barriers. The city has a complex park system, including national, state, and city parks. The largest municipal park is Pelham Bay Park in the Bronx. New York City is the most populous city in the United States, with 8,804,190 residents as of 2020. The city has a diverse population, with 30.9% White (non-Hispanic), 28.7% Hispanic or Latino, 20.2% Black or African American (non-Hispanic), 15.6% Asian, and 0.2% Native American (non-Hispanic). Approximately 37% of the city's population is foreign-born, with no single country or region dominating. The Chinese population is the fastest-growing nationality in New York State, and the city has the highest total Asian population of any U.S. city proper.\n",
      ">[Level 1] Current response: ANSWER: 9\n",
      "\n",
      "This summary was selected because it provides the demographic breakdown of New York City by ethnicity, including percentages for White (non-Hispanic), Hispanic or Latino, Black or African American (non-Hispanic), Asian, and Native American (non-Hispanic) populations. It also discusses the foreign-born population and the largest sources of foreign-born individuals in the city.\n",
      ">[Level 1] Selected node: [9]/[9]\n",
      ">[Level 1] Node [9] Summary text: A total of 3.4% of the non-Hispanic population identified with more than one race. Throughout its history, New York has been a major port of entry for immigrants into the United States. More than 12 million European immigrants were received at Ellis Island between 1892 and 1924. The term \"melting pot\" was first coined to describe densely populated immigrant neighborhoods on the Lower East Side. By 1900, Germans constituted the largest immigrant group, followed by the Irish, Jews, and Italians. In 1940, Whites represented 92% of the city's population.Approximately 37% of the city's population is foreign born, and more than half of all children are born to mothers who are immigrants as of 2013. In New York, no single country or region of origin dominates. The ten largest sources of foreign-born individuals in the city as of 2011 were the Dominican Republic, China, Mexico, Guyana, Jamaica, Ecuador, Haiti, India, Russia, and Trinidad and Tobago, while the Bangladeshi-born immigrant population has become one of the fastest growing in the city, counting over 74,000 by 2011.Asian Americans in New York City, according to the 2010 census, number more than one million, greater than the combined totals of San Francisco and Los Angeles. New York contains the highest total Asian population of any U.S. city proper. The New York City borough of Queens is home to the state's largest Asian American population and the largest Andean (Colombian, Ecuadorian,\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: A total of 3.4% of the non-Hispanic population identified with more than one race. Throughout its history, New York has been a major port of entry for immigrants into the United States. More than 1...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "logger = LlamaLogger()\n",
    "query_engine = index.as_query_engine(\n",
    "    service_context=service_context_gpt4,\n",
    "    llama_logger=logger,\n",
    "    verbose=True,\n",
    ")\n",
    "response_gpt4 = query_engine.query(\n",
    "    \"What is the demographic breakdown of NYC by ethnicity?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ce6dca6e-6c1b-4d4e-98a0-238a2fd37bef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"The context information does not provide a complete demographic breakdown of NYC by ethnicity. However, it mentions that in 1940, Whites represented 92% of the city's population, and approximately 37% of the city's population is foreign-born as of 2013. The ten largest sources of foreign-born individuals in the city as of 2011 were the Dominican Republic, China, Mexico, Guyana, Jamaica, Ecuador, Haiti, India, Russia, and Trinidad and Tobago. Additionally, the Asian American population in New York City numbers more than one million according to the 2010 census.\""
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "17221cd8-c782-41c6-8775-293c828557f6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: What is the demographic breakdown of NYC by ethnicity?\n",
      ">[Level 0] Current response: \n",
      "ANSWER: 5. This summary provides information on the population of New York City, including the percentage of White (non-Hispanic), Hispanic or Latino, Black or African American (non-Hispanic), Asian, and Native American (non-Hispanic) residents. It also mentions the city's large foreign-born population and the fastest-growing nationality in the state. This information is directly relevant to the question of the demographic breakdown of NYC by ethnicity.\n",
      ">[Level 0] Selected node: [5]/[5]\n",
      ">[Level 0] Node [5] Summary text: New York City is a diverse and multicultural metropolis, with a population of over 8.4 million people. The city has the largest European and non-Hispanic white population of any American city, as well as the largest Jewish population of any city in the world. The city is also home to a significant number of Asian, African, and Latin American immigrants, with various ethnic enclaves throughout the boroughs. New York City has been described as the gay capital of the world, with a large LGBTQ population and a history of activism and progress in LGBTQ rights. The city is also a global hub of business and commerce, with a strong presence in industries such as finance, healthcare, media, fashion, and technology.\n",
      ">[Level 1] Current response: \n",
      "ANSWER: 2. People identifying ancestry from Spain numbered 30,838 total in 2010. People of Norwegian and Swedish descent both stood at about 20,000 each, while people of Czech, Lithuanian, Portuguese, Scotch-Irish, and Welsh descent all numbered between 12,000 and 14,000. Arab Americans number over 160,000 in New York City, with the highest concentration in Brooklyn. Central Asians, primarily Uzbek Americans, are a rapidly growing segment of the city's non-Hispanic White population, enumerating over 30,000, and including more than half of all Central Asian immigrants to the United States, most settling in Queens or Brooklyn. Albanian Americans are most highly concentrated in the Bronx, while Astoria, Queens is the epicenter of American Greek culture as well as the Cypriot community.\n",
      "\n",
      "This summary was selected because it provides a detailed breakdown of the ethnicities present in New York City, including the number of people from each group. It also provides information on the concentration of each group in different boroughs.\n",
      ">[Level 1] Selected node: [2]/[2]\n",
      ">[Level 1] Node [2] Summary text: people. Ukrainian and Scottish Americans numbered 55,000 and 35,000, respectively. People identifying ancestry from Spain numbered 30,838 total in 2010.People of Norwegian and Swedish descent both stood at about 20,000 each, while people of Czech, Lithuanian, Portuguese, Scotch-Irish, and Welsh descent all numbered between 12,000 and 14,000. Arab Americans number over 160,000 in New York City, with the highest concentration in Brooklyn. Central Asians, primarily Uzbek Americans, are a rapidly growing segment of the city's non-Hispanic White population, enumerating over 30,000, and including more than half of all Central Asian immigrants to the United States, most settling in Queens or Brooklyn. Albanian Americans are most highly concentrated in the Bronx, while Astoria, Queens is the epicenter of American Greek culture as well as the Cypriot community.\n",
      "New York is also home to the highest Jewish population of any city in the world, numbering 1.6 million in 2022, more than Tel Aviv and Jerusalem combined. In the borough of Brooklyn, an estimated 1 in 4 residents is Jewish. The city's Jewish communities are derived from many diverse sects, predominantly from around the Middle East and Eastern Europe, and including a rapidly growing Orthodox Jewish population, also the largest outside Israel.The metropolitan area is also home to 20% of the nation's Indian Americans and at least 20 Little India enclaves, and 15% of all Korean Americans and four\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: people. Ukrainian and Scottish Americans numbered 55,000 and 35,000, respectively. People identifying ancestry from Spain numbered 30,838 total in 2010.People of Norwegian and Swedish descent both ...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(\n",
    "    service_context=service_context_gpt3,\n",
    "    llama_logger=logger,\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "response_gpt3 = query_engine.query(\n",
    "    \"What is the demographic breakdown of NYC by ethnicity?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fc2a01b7-23be-4b77-8acb-f6f612f9e2c0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The demographic breakdown of NYC by ethnicity is as follows: Ukrainian Americans (55,000), Scottish Americans (35,000), Spanish Americans (30,838), Norwegian Americans (20,000), Swedish Americans (20,000), Czech Americans (12,000-14,000), Lithuanian Americans (12,000-14,000), Portuguese Americans (12,000-14,000), Scotch-Irish Americans (12,000-14,000), Welsh Americans (12,000-14,000), Arab Americans (over 160,000), Central Asian Americans (over 30,000), Albanian Americans (concentrated in the Bronx), Greek Americans (concentrated in Astoria, Queens), Cypriot Americans (concentrated in Astoria, Queens), Jewish Americans (1.6 million), Indian Americans (20%), Korean Americans (15%), and other ethnicities.'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "537a13b6-6f5c-474e-90e9-cfe83a359dc4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: Why is congestion pricing in NYC being introduced?\n",
      ">[Level 0] Current response: ANSWER: (9) This summary was selected because it mentions congestion pricing in New York City, stating that it is set to go into effect in 2022. The other summaries do not mention congestion pricing or its purpose.\n",
      ">[Level 0] Selected node: [9]/[9]\n",
      ">[Level 0] Node [9] Summary text: New York City is known for its efforts in environmental sustainability, with initiatives such as the Citi Bike program, green office buildings, and a commitment to reducing greenhouse gas emissions. The city's water supply comes from the protected Catskill Mountains watershed, providing pure drinking water without the need for purification. New York City's air quality is within the recommended limits of the World Health Organization, and efforts are being made to revitalize polluted areas like Newtown Creek. The city's government operates under a strong mayor-council system, with the current mayor being Eric Adams. The Democratic Party holds the majority of public offices in the city.\n",
      "\n",
      "New York City's extensive transportation system includes the largest rapid transit system in the world, the New York City Subway, as well as the largest public bus fleet in North America. The city is served by three major airports, with plans to expand and improve facilities at LaGuardia Airport. The Staten Island Ferry is the world's busiest ferry route, and other ferry systems connect various parts of the city. Taxis, vehicles for hire, and trams are also part of the city's transportation infrastructure. Despite heavy reliance on public transit, streets and highways remain a defining feature of New York City, with congestion pricing set to go into effect in 2022\n",
      ">[Level 1] Current response: ANSWER: 10\n",
      "\n",
      "This summary was selected in relation to the question because it directly mentions congestion pricing in New York City and its planned implementation in 2022. The summary also discusses the extensive web of freeways and parkways, as well as the daily traffic congestion that occurs, particularly during rush hour. This context implies that congestion pricing is being introduced to address the traffic congestion issue in the city.\n",
      ">[Level 1] Selected node: [10]/[10]\n",
      ">[Level 1] Node [10] Summary text: in September 2016, to shuttle riders between the Jersey Shore and Manhattan, anticipated to start service in 2017; this would be the largest vessel in its class.\n",
      "\n",
      "\n",
      "=== Taxis, vehicles for hire, and trams ===\n",
      "\n",
      "Other features of the city's transportation infrastructure encompass 13,587 yellow taxicabs; other vehicle for hire companies; and the Roosevelt Island Tramway, an aerial tramway that transports commuters between Roosevelt Island and Manhattan Island.\n",
      "\n",
      "\n",
      "=== Streets and highways ===\n",
      "\n",
      "Despite New York's heavy reliance on its vast public transit system, streets are a defining feature of the city. The Commissioners' Plan of 1811 greatly influenced the city's physical development. Several of the city's streets and avenues, including Broadway, Wall Street, Madison Avenue, and Seventh Avenue are also used as metonyms for national industries there: the theater, finance, advertising, and fashion organizations, respectively.\n",
      "New York City also has an extensive web of freeways and parkways, which link the city's boroughs to each other and to North Jersey, Westchester County, Long Island, and southwestern Connecticut through various bridges and tunnels. Because these highways serve millions of outer borough and suburban residents who commute into Manhattan, it is quite common for motorists to be stranded for hours in traffic congestion that are a daily occurrence, particularly during rush hour. Congestion pricing in New York City will go into effect in 2022 at the\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: in September 2016, to shuttle riders between the Jersey Shore and Manhattan, anticipated to start service in 2017; this would be the largest vessel in its class.\n",
      "\n",
      "\n",
      "=== Taxis, vehicles for hire, and...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "logger = LlamaLogger()\n",
    "query_engine = index.as_query_engine(\n",
    "    service_context=service_context_gpt4,\n",
    "    llama_logger=logger,\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "response_gpt4 = query_engine.query(\n",
    "    \"Why is congestion pricing in NYC being introduced?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d8497f8f-512f-4165-bc3b-63c786d70d88",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The context information does not explicitly state why congestion pricing is being introduced in NYC.'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e442b1e5-2523-4f6a-a790-91c567470059",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"in September 2016, to shuttle riders between the Jersey Shore and Manhattan, anticipated to start service in 2017; this would be the largest vessel in its class.\\n\\n\\n=== Taxis, vehicles for hire, and trams ===\\n\\nOther features of the city's transportation infrastructure encompass 13,587 yellow taxicabs; other vehicle for hire companies; and the Roosevelt Island Tramway, an aerial tramway that transports commuters between Roosevelt Island and Manhattan Island.\\n\\n\\n=== Streets and highways ===\\n\\nDespite New York's heavy reliance on its vast public transit system, streets are a defining feature of the city. The Commissioners' Plan of 1811 greatly influenced the city's physical development. Several of the city's streets and avenues, including Broadway, Wall Street, Madison Avenue, and Seventh Avenue are also used as metonyms for national industries there: the theater, finance, advertising, and fashion organizations, respectively.\\nNew York City also has an extensive web of freeways and parkways, which link the city's boroughs to each other and to North Jersey, Westchester County, Long Island, and southwestern Connecticut through various bridges and tunnels. Because these highways serve millions of outer borough and suburban residents who commute into Manhattan, it is quite common for motorists to be stranded for hours in traffic congestion that are a daily occurrence, particularly during rush hour. Congestion pricing in New York City will go into effect in 2022 at the\""
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt4.source_nodes[0].source_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b5457909-4ee3-4a4a-87d9-14006a45e70a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: Why is congestion pricing in NYC being introduced?\n",
      ">[Level 0] Current response: \n",
      "ANSWER: 8. This summary provides information about the city's transportation infrastructure, including the introduction of congestion pricing in 2022. It also mentions the city's extensive public transit system, which is heavily relied upon, and the plans to expand and improve facilities at LaGuardia Airport. This information is relevant to the question of why congestion pricing is being introduced in NYC.\n",
      ">[Level 0] Selected node: [8]/[8]\n",
      ">[Level 0] Node [8] Summary text: New York City is home to numerous cultural institutions, historic sites, and diverse cuisine influenced by its immigrant history. The city is known for its street parades, distinctive regional accent, and sports teams across various leagues. Environmental issues are a concern due to the city's size and density, but efforts have been made to reduce its environmental impact and carbon footprint. New York City has a high rate of public transit use, a growing number of cyclists, and is considered the most walkable large city in the United States.\n",
      ">[Level 1] Current response: \n",
      "ANSWER: 8. Environment == Environmental impact reduction === New York City has focused on reducing its environmental impact and carbon footprint. Mass transit use in New York City is the highest in the United States. This summary was selected because it explains how New York City is attempting to reduce its environmental impact, which includes introducing congestion pricing to reduce traffic and emissions.\n",
      ">[Level 1] Selected node: [8]/[8]\n",
      ">[Level 1] Node [8] Summary text: the city. The city is well known for its links to basketball, which is played in nearly every park in the city by local youth, many of whom have gone on to play for major college programs and in the NBA.\n",
      "\n",
      "\n",
      "==== Soccer ====\n",
      "In soccer, New York City is represented by New York City FC of Major League Soccer, who play their home games at Yankee Stadium and the New York Red Bulls, who play their home games at Red Bull Arena in nearby Harrison, New Jersey. NJ/NY Gotham FC also plays their home games in Red Bull Arena, representing the metropolitan area in the National Women's Soccer League. Historically, the city is known for the New York Cosmos, the highly successful former professional soccer team which was the American home of Pelé. A new version of the New York Cosmos was formed in 2010, and most recently played in the third-division National Independent Soccer Association before going on hiatus in January 2021. New York was a host city for the 1994 FIFA World Cup and will be one of eleven US host cities for the 2026 FIFA World Cup.\n",
      "\n",
      "\n",
      "==== Tennis ====\n",
      "The annual United States Open Tennis Championships is one of the world's four Grand Slam tennis tournaments and is held at the National Tennis Center in Flushing Meadows–Corona Park, Queens. The New York City Marathon, which courses through all five boroughs, is the world's largest running marathon, with 51,394 finishers in 2016 and 98,247 applicants for the 2017 race. The Millrose Games is\n",
      "\u001b[36;1m\u001b[1;3m>[Level 1] Got node text: the city. The city is well known for its links to basketball, which is played in nearly every park in the city by local youth, many of whom have gone on to play for major college programs and in th...\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(\n",
    "    service_context=service_context_gpt3, llama_logger=logger, verbose=True\n",
    ")\n",
    "\n",
    "response_gpt3 = query_engine.query(\n",
    "    \"Why is congestion pricing in NYC being introduced?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9e0cd9e4-9dd4-49ae-8019-9fb16d11ab34",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Congestion pricing in New York City is being introduced in order to reduce traffic congestion and raise revenue for public transportation improvements. The plan is to charge drivers a fee for entering certain parts of the city during peak hours, with the goal of reducing traffic and encouraging people to use public transportation instead.'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(response_gpt3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "385b6e06-263a-4aba-a459-e72e21f2150d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index",
   "language": "python",
   "name": "llama_index"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
